package cn.hc.case2;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/10/19 11:18
 */
public class DistributeLockTest {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        DistributeLock lock1 = new DistributeLock();
        DistributeLock lock2 = new DistributeLock();

        new Thread(() -> {
            try {
                lock1.zklock();
                System.out.println("线程1 启动，获取到锁");
                Thread.sleep(5000);
                lock1.unZkLock();
                System.out.println("线程1 释放锁");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {

            try {
                lock2.zklock();
                System.out.println("线程2 启动，获取到锁");
                Thread.sleep(5000);
                lock2.unZkLock();
                System.out.println("线程2 释放锁");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
